const models = require('../db')
const express = require('express')
const router = express.Router()
const mysql = require('mysql')
const $sql = require('../sqlMap')
// 连接数据库
// const conn = mysql.createConnection(models.mysql)
const conn = mysql.createConnection(models.mysql_config)
// const conn = mysql.createConnection(models.handleDisconnection)
// const conn = models.handleDisconnection
// handleDisconnection()
// conn.connect()

const jsonWrite = function (res, ret) {
    if (typeof ret === 'undefined') {
        res.json({
            code: '1',
            msg: '操作失败'
        })
    } else {
        res.json({
            "code": '200',
            "msg": 'ok'
        })
    }
}
const jsonWriteShow = function (res, ret) {
    if (typeof ret === 'undefined') {
        res.json({
            code: '1',
            msg: '操作失败'
        })
    } else {
        res.json({
                code: '200',
                msg: 'ok',
                data:{
                    a:10,
                    b:20,
                    c:[1,2,34],
                    ret
                },
                
            }

        )
    }
}


//接口：查詢信息
router.get('/showlist', (req, res) => {
    const sql = $sql.List.showAll
    const params = req.query
    console.log('查询', params)
        conn.query(sql, function (err, result) {
            if (err) {
                console.log(err)
            }
            if (result) {
                jsonWriteShow(res, result)
            }
        })
        conn.close;
})

// 接口：增加信息
router.post('/addlist', (req, res) => {
    const sql = $sql.List.add
    const params = req.body
    const time = new Date()
    const ass=params.ass?params.ass : 0
    const comment=params.comment?params.comment : 0
    const imgurl =params.imgurl?params.imgurl : "https://p5.toutiaoimg.com/img/pgc-image/429004ce6fd64e14b30a70b340165b96~720x380_cs.webp"
    const url=params.url?params.url : ''
    console.log('添加', params,ass,comment, imgurl, url)
    conn.query(sql, [params.title, ass, comment,time, imgurl, url], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res, result)
        }
        
    })
    conn.close;
})



// 接口：删除信息
router.delete('/deletelist', (req, res) => {
    const sql = $sql.List.del
    const params = req.query
    console.log('删除', params)
    conn.query(sql, [params.id], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res, result)
        }
    })
    conn.close;
})



// 更新信息
router.post('/updatelist', (req, res) => {
    const sql = $sql.List.update
    const params = req.body
    const time = new Date()
    const read=params.read?params.read : 0
    const comment=params.comment?params.comment : 0
    const imgurl =params.imgurl?params.imgurl : "https://p5.toutiaoimg.com/img/pgc-image/429004ce6fd64e14b30a70b340165b96~720x380_cs.webp"
    const url=params.url?params.url : ''
    console.log('更新')
    conn.query(sql, [params.title, read, comment, time, imgurl, url, params.id], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res, result)
        }
    })
    // conn.close;
})





module.exports = router